#!/usr/bin/env node

/*
	ES6-capable adaptive minifier
*/

!function () {
	'use strict';
	
	Error.stackTraceLimit = Infinity;
	
	var esprima = require('esprima');
	var escodegen = require('escodegen');
	var UglifyJS = require("uglify-js");
	
	var code = '';
	process.stdin.on('data', function (data) {
		code += data;
	});
	process.stdin.on('end', function (err) {
		try {
			console.log(UglifyJS.minify(code, {
				mangle: {
					toplevel: true
					, screw_ie8: true
				}
				, fromString: true
			}).code);
			return;	
		} catch (e) {
			//console.warn(e);
		}
		
		
		// UglifyJS failed, just remove spaces
		
		var tree = esprima.parse(code, {
			
		});
		//console.log('error:' + err);
		console.log(escodegen.generate(tree, {
			format: {
				json: false,
				renumber: false,
				hexadecimal: false,
				quotes: 'single',
				escapeless: false,
				compact: true,
				parentheses: true,
				semicolons: false,
				safeConcatenation: true
		    },
			moz: {
				starlessGenerator: false,
				parenthesizedComprehensionBlock: false,
				comprehensionExpressionStartsWithAssignment: false
			},
			parse: null,
			comment: false
		}));
	});
	process.stdin.resume();
	
}();

// vim: set ts=4 noet ai ft=javascript
